package com.suray.attendance.contacts.utils;

import com.alibaba.fastjson.JSON;
import com.aliyun.dingtalkworkflow_1_0.models.QuerySchemaByProcessCodeHeaders;
import com.aliyun.dingtalkworkflow_1_0.models.QuerySchemaByProcessCodeRequest;
import com.aliyun.dingtalkworkflow_1_0.models.QuerySchemaByProcessCodeResponse;
import com.aliyun.dingtalkworkflow_1_0.models.QuerySchemaByProcessCodeResponseBody;
import com.aliyun.tea.TeaException;
import com.aliyun.teautil.models.RuntimeOptions;
import com.suray.attendance.common.constant.DingTalkConstant;
import com.suray.attendance.common.dingding.DingTalkClientUtils;

import java.util.Map;

/**
 * @Author: jianglinyu
 * @CreateTime: 2023-07-03  15:42
 * @Description: 根据ProcessCode获取审批模板的信息
 * @Version: 1.0
 */
public class SchemaUtil {

    /**
     * @description:  根据表单模板的code获取模板的详细信息
     * @author: jianglinyu
     * @date: 2023/7/5 10:55
     **/
    public static String getSchema(){
        com.aliyun.dingtalkworkflow_1_0.Client client = null;
        try {
            client = DingTalkClientUtils.createClient();
            QuerySchemaByProcessCodeHeaders querySchemaByProcessCodeHeaders = new QuerySchemaByProcessCodeHeaders();
            querySchemaByProcessCodeHeaders.xAcsDingtalkAccessToken = AccessTokenUtil.getToken();
            QuerySchemaByProcessCodeRequest querySchemaByProcessCodeRequest = new QuerySchemaByProcessCodeRequest()
                .setProcessCode(DingTalkConstant.BUSINESS_TRIP_PROCESS_CODE);
            QuerySchemaByProcessCodeResponse querySchemaByProcessCodeResponse = client.querySchemaByProcessCodeWithOptions(querySchemaByProcessCodeRequest, querySchemaByProcessCodeHeaders, new RuntimeOptions());
            Map<String, String> headers = querySchemaByProcessCodeResponse.getHeaders();
            QuerySchemaByProcessCodeResponseBody body = querySchemaByProcessCodeResponse.getBody();
            QuerySchemaByProcessCodeResponseBody.QuerySchemaByProcessCodeResponseBodyResult result = body.getResult();
            return JSON.toJSONString(result);
        } catch (TeaException err) {
            if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
                // err 中含有 code 和 message 属性，可帮助开发定位问题
                err.printStackTrace();
            }
        } catch (Exception _err) {
            TeaException err = new TeaException(_err.getMessage(), _err);
            if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
                // err 中含有 code 和 message 属性，可帮助开发定位问题
            }
        }
        return null;
    }
    // {"appType":0,"appUuid":"dingaaf9e1c8d740ce3935c2f4657eb6378f","bizType":"","creatorUserId":"16654606273349492","engineType":0,"formCode":"PROC-BD59CBB0-30DE-44F3-8F9B-9C1B1048299C","formUuid":"FORM-D37E63CE-8EEA-4D91-8E96-51A24D859675","gmtCreate":"2022-12-22T13:16Z","gmtModified":"2023-07-04T11:49Z","listOrder":2618,"memo":"erp请购单审批","name":"ERP请购单生成后发起审批","ownerIdType":"orgId","procType":"inner","schemaContent":{"icon":"common","items":[{"componentName":"TextField","props":{"bizAlias":"","holidayOptions":[],"id":"TextField-K2AD4O5B","label":"请购单号","placeholder":"请输入","push":{},"staffStatusEnabled":false}},{"componentName":"TextField","props":{"bizAlias":"","holidayOptions":[],"id":"TextField_7LNFSKF86EO0","label":"请购人","placeholder":"请输入","push":{},"required":false,"staffStatusEnabled":false}},{"componentName":"DDSelectField","props":{"behaviorLinkage":[],"bizAlias":"","holidayOptions":[],"id":"DDSelectField_JOM639UOY5C0","label":"单据性质","options":["{\"value\":\"请购单\",\"key\":\"option_0\"}","{\"extension\":{\"image\":\"\"},\"value\":\"项目请购\",\"key\":\"option_JFCYTDCL0IO0\"}","{\"value\":\"研发请购\",\"key\":\"option_1\"}","{\"value\":\"运营请购\",\"key\":\"option_2\"}","{\"extension\":{\"image\":\"\"},\"value\":\"费用请购\",\"key\":\"option_WYKR58355YO0\"}"],"placeholder":"请选择","push":{},"required":true,"staffStatusEnabled":false}},{"componentName":"DDSelectField","props":{"behaviorLinkage":[],"bizAlias":"","holidayOptions":[],"id":"DDSelectField_V54T0BJDW000","label":"所属公司","options":["{\"value\":\"上海哥伦布智能工厂\",\"key\":\"option_0\"}","{\"value\":\"上海哥伦布工厂\",\"key\":\"option_1\"}","{\"value\":\"速锐智能工厂\",\"key\":\"option_2\"}"],"placeholder":"请选择","push":{},"required":true,"staffStatusEnabled":false}},{"componentName":"DDAttachment","props":{"bizAlias":"","holidayOptions":[],"id":"DDAttachment_1BK8QXB9LNPC0","label":"请购明细","push":{},"required":false,"staffStatusEnabled":false}},{"componentName":"DDAttachment","props":{"bizAlias":"","holidayOptions":[],"id":"DDAttachment_QCLW5RY38Y80","label":"相关图纸","push":{},"required":false,"staffStatusEnabled":false}}],"title":"ERP请购单生成后发起审批"},"status":"PUBLISHED","visibleRange":"PRIVATE"}
    public static void main(String[] args) {
        System.out.println(getSchema());
    }
}